<?php
session_start();

include("language.php");

$_SESSION['curPage'] = 'contact.php';
$error = '';

$message = '';
$subject = '';
$email = '';

if(isset($_POST['message']) AND isset($_POST['email']) AND isset($_POST['subject']))
{
    $message = $_POST['message'];
    $subject = $_POST['subject'];
    $email = $_POST['email'];
    
    if(strlen($_POST['email']) == 0)
    {
        $error .= '<li>' . $contact_badEmail . '</li>';
    }
    
    if(strlen($_POST['subject']) < 5)
    {
        $error .= '<li>' . $contact_badSubject . '</li>';
    }
    
    if(strlen($_POST['message']) < 10 OR strlen($_POST['message'] > 150))
    {
        $error .= '<li>' . $contact_badMessage . '</li>';
    }
    
    if(strlen($error) == 0)
    {
        $message = $message . "\n\n=============================\nContact Mail: " . $email;
        $headers = "From: D3 Lottery <bot@d3lottery.com>";
        mail('contact@d3lottery.com', $subject, $message, $headers);
        $message = '';
        $subject = '';
        $email = '';
    }
}
else 
{
    $error .= ' ';
}
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Contact</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="d_body">

            <?php include('header.php'); ?>
            <?php include("menuBar.php"); ?>   
            
            <h1>Contact</h1>
            <p><?php echo $register_allFieldsRequired; ?></p>
            <section class="main_section">
                
                <?php 
                    if(strlen($error) == 0)
                    {
                        echo '<p class="text_success">' . $contact_messageSent . '</p>';
                    }
                    else 
                    {
                        echo '<ul>' . $error . '</ul>';
                    }
                ?>
                
                <form method="post" action="contact.php">
                    <table class="drawTable">
                        <tr>
                            <td><label><?php echo $contact_yourMail; ?></label></td>
                            <td><input class="large" name="email" type="mail" value="<?php echo $email; ?>" /></td>
                        </tr>

                        <tr>
                            <td><label><?php echo $contact_subject; ?></label></td>
                            <td><input class="large" name="subject" type="text" value="<?php echo $subject; ?>" /></td>
                        </tr>
                        
                        <tr>
                            <td><label><?php echo $contact_message; ?></label></td>
                            <td><textarea rows="10" name="message"><?php echo $message; ?></textarea></td>
                        </tr>
                        
                        <tr>
                            <td class="cellTextCenter" colspan="2"><input type="submit" value="<?php echo $contact_send; ?>" /></td>
                        </tr>
                    </table>
                </form>
            </section>
        </div>
        <?php include('footer.php'); ?>
    </body>
</html>
